package com.java.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


public class TCPServerC {
	private ServerSocket ss;
	private static int i=1;
	
	public TCPServerC(){
		try {
			ss=new ServerSocket(8888);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void startService()throws IOException{
		while(true){
			Socket s=ss.accept();
			new ServerThread(s,i).start();
			i++;
		}
	}
	
	private static class ServerThread extends Thread{
		private Socket s;
		private int i;
		
		public ServerThread(Socket s,int i){
			this.s=s;
			this.i=i;
		}
		
		public void run(){
			BufferedReader br=null;
			PrintWriter pw=null;
			
			try {
				br=new BufferedReader(new InputStreamReader(s.getInputStream()));
				String str=br.readLine();
				System.out.println(i+":"+s.getInetAddress()+":"+str);
				str=str.toUpperCase();
				pw = new PrintWriter(s.getOutputStream());
				pw.println(str);
				pw.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args){
		try {
			new TCPServerC().startService();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}